﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Security.AccessControl;
using System.Threading.Tasks;
using System.Xml.Linq;
using EcoSystem.Permissions;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Volo.Abp;
using Volo.Abp.Application.Dtos;
using static System.Runtime.InteropServices.JavaScript.JSType;

namespace EcoSystem;

/// <summary>
/// 资源文件管理
/// </summary>
[Area(EcoSystemRemoteServiceConsts.ModuleName)]
[RemoteService(Name = EcoSystemRemoteServiceConsts.RemoteServiceName)]
[Route("api/Platform/Resources/file")]
public class FileResourceController : EcoSystemController, IFileResourceAppService
{
    private readonly IFileResourceAppService _service;

    public FileResourceController(IFileResourceAppService service)
    {
        _service = service;
    }

    /// <summary>
    /// 创建
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpPost]
    public async Task<FileResourceDto> CreateAsync(CreateOrUpdateFileResourceDto input)
    {
        return await _service.CreateAsync(input);
    }

    /// <summary>
    /// 获取详情
    /// </summary>
    /// <param name="id"></param>
    /// <returns></returns>
    [HttpGet]
    [Route("{id}")]
    public async Task<FileResourceDto> GetAsync(Guid id)
    {
        return await _service.GetAsync(id);
    }

    /// <summary>
    /// 修改
    /// </summary>
    /// <param name="id"></param>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpPut]
    [Route("{id}")]
    public async Task<FileResourceDto> UpdateAsync(Guid id, CreateOrUpdateFileResourceDto input)
    {
        return await _service.UpdateAsync(id, input);
    }


    /// <summary>
    /// 列表查询
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpGet]
    public async Task<PagedResultDto<FileResourceDto>> GetListAsync(GetFileResourceInput input)
    {
        return await _service.GetListAsync(input);
    }

    /// <summary>
    /// 获取所有
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpGet]
    [Route("all")]
    public async Task<List<FileResourceDto>> GetAllListAsync(GetFileResourceInput input)
    {
        return await _service.GetAllListAsync(input);
    }

    /// <summary>
    /// 删除
    /// </summary>
    /// <param name="id"></param>
    /// <returns></returns>
    [HttpDelete]
    [Route("{id}")]
    public async Task DeleteAsync(Guid id)
    {
        await _service.DeleteAsync(id);
    }

    /// <summary>
    /// 上传文件
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpPost]
    [Route("upload")]
    public async Task<FileResourceDto> UploadFileAsync([FromForm] UploadFileResourceInputDto input)
    {
        return await _service.UploadFileAsync(input);
    }


    /// <summary>
    ///  上传文件 - AllowAnonymous
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    /// <exception cref="UserFriendlyException"></exception>
    [HttpPost]
    [AllowAnonymous]
    [Route("/api/Member/memberExpertApply/upload")]
    public async Task<FileResourceDto> UploadApplyFileAsync([FromForm] UploadFileResourceInputDto input)
    {
        var ext = Path.GetExtension(input.File.FileName)?.ToLower();
        if (ext != ".jpg" && ext != ".jpeg" && ext != ".png" && ext != ".bmp")
        {
            throw new UserFriendlyException("不支持的文件格式");
        }
        if (input.File.Length > 1024 * 1024 * 2)
        {
            throw new UserFriendlyException("上传文件不允许超过2MB");
        }
        return await _service.UploadFileAsync(input);
    }

    /// <summary>
    /// 下载文件
    /// </summary>
    /// <param name="id"></param>
    /// <returns></returns>
    [HttpGet]
    [Route("/api/file/download/{id}")]
    [AllowAnonymous]
    public async Task<FileResult> DownFileAsync(Guid id)
    {
        var data = await _service.GetAsync(id);
        var filePath = Path.Combine(AppContext.BaseDirectory, data.FilePath);
        string mimeType = FileContentType.GetMimeType(data.Extension);
        FileStreamResult result = new FileStreamResult(new FileStream(filePath, FileMode.Open), mimeType);
        return result;
    }


    /// <summary>
    /// 
    /// </summary>
    /// <param name="path"></param>
    /// <returns></returns>
    [HttpGet]
    [Route("/api/file/{*name}")]
    [AllowAnonymous]
    public async Task<FileResult> DownFileForPathAsync(string path)
    {
        var data = await _service.GetForNameAsync(path);
        var filePath = Path.Combine(AppContext.BaseDirectory, data.FilePath);
        string mimeType = FileContentType.GetMimeType(data.Extension);
        FileStreamResult result = new FileStreamResult(new FileStream(filePath, FileMode.Open), mimeType);
        return result;
    }

    [NonAction]
    public async Task<FileResourceDto> GetForNameAsync([FromRoute(Name = "name")] string objectName)
    {
        return await _service.GetForNameAsync(objectName);
    }
}
